﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Jalal.Library.Data;
using System.Data.Common;
using System.Data;
using Jalal.Library.Utility;

namespace BCGC.DAL
{
    public class PrivateMessageData
    {
        public static int CreatePrivateMessage(int senderID, string senderName, int receiverID, string receiverName, string receiverEmailID, DateTime sendDate, string subject, string content, bool isSendEmail, bool isMarketPlaceMessage, bool isRead, bool isActive, bool isShowInBox, bool isShowOutBox, bool isReply, int replyToMessageID)
        {
            object id = null;
            DataMiner dataMiner = DataFactory.GetDataMiner();
            DbCommand command = DataFactory.GetCommand();
            command.CommandText = "CSP_CreatePrivateMessage";

            dataMiner.AddParameter(command, "@SenderId", DbType.Int32, 0, ParameterDirection.Input,senderID);
            dataMiner.AddParameter(command, "@SenderName", DbType.String, 100, ParameterDirection.Input, senderName);
            dataMiner.AddParameter(command, "@ReceiverID", DbType.Int32, 0, ParameterDirection.Input, receiverID);
            dataMiner.AddParameter(command, "@ReceiverName", DbType.String, 100, ParameterDirection.Input, receiverName);
            dataMiner.AddParameter(command, "@ReceiverEmailID", DbType.String, 100, ParameterDirection.Input, receiverEmailID);
            dataMiner.AddParameter(command, "@SendDate", DbType.DateTime, 0, ParameterDirection.Input, sendDate);
            dataMiner.AddParameter(command, "@Subject", DbType.String, 100, ParameterDirection.Input, subject);
            dataMiner.AddParameter(command, "@MailContent", DbType.String, 0, ParameterDirection.Input, content);
            dataMiner.AddParameter(command, "@IsSendEmail", DbType.Boolean, 0, ParameterDirection.Input, isSendEmail);
            dataMiner.AddParameter(command, "@IsMarketPlaceMessage", DbType.Boolean, 0, ParameterDirection.Input, isMarketPlaceMessage);
            dataMiner.AddParameter(command, "@IsRead", DbType.Boolean, 0, ParameterDirection.Input, isRead);
            dataMiner.AddParameter(command, "@IsActive", DbType.Boolean, 0, ParameterDirection.Input, isActive);
            dataMiner.AddParameter(command, "@IsShowInBox", DbType.Boolean, 0, ParameterDirection.Input, isShowInBox);
            dataMiner.AddParameter(command, "@IsShowOutBox", DbType.Boolean, 0, ParameterDirection.Input, isShowOutBox);
            dataMiner.AddParameter(command, "@IsReply", DbType.Boolean, 0, ParameterDirection.Input, isReply);
            dataMiner.AddParameter(command, "@ReplyToMessageID", DbType.Int32, 0, ParameterDirection.Input, replyToMessageID);
            dataMiner.AddParameter(command, "ReturnValue", DbType.Int32, 0, ParameterDirection.ReturnValue, null);
            dataMiner.Execute(command, out id);

            return (int)CommonUtility.FilterNull(id, typeof(int));
        }

        public static bool UpdatePrivateMessage(int messageID, int senderID, string senderName, int receiverID, string receiverName, string receiverEmailID, DateTime sendDate, string subject, string content, bool isSendEmail, bool isMarketPlaceMessage, bool isRead, bool isActive, bool isShowInBox, bool isShowOutBox, bool isReply, int replyToMessageID)
        {
            object count = null;
            DataMiner dataMiner = DataFactory.GetDataMiner();
            DbCommand command = DataFactory.GetCommand();
            command.CommandText = "CSP_UpdatePrivateMessage";
            dataMiner.AddParameter(command, "@MessageId", DbType.Int32, 0, ParameterDirection.Input, messageID);
            dataMiner.AddParameter(command, "@SenderId", DbType.Int32, 0, ParameterDirection.Input, senderID);
            dataMiner.AddParameter(command, "@SenderName", DbType.String, 100, ParameterDirection.Input, senderName);
            dataMiner.AddParameter(command, "@ReceiverID", DbType.Int32, 0, ParameterDirection.Input, receiverID);
            dataMiner.AddParameter(command, "@ReceiverName", DbType.String, 100, ParameterDirection.Input, receiverName);
            dataMiner.AddParameter(command, "@ReceiverEmailID", DbType.String, 100, ParameterDirection.Input, receiverEmailID);
            dataMiner.AddParameter(command, "@SendDate", DbType.DateTime, 0, ParameterDirection.Input, sendDate);
            dataMiner.AddParameter(command, "@Subject", DbType.String, 100, ParameterDirection.Input, subject);
            dataMiner.AddParameter(command, "@MailContent", DbType.String, 0, ParameterDirection.Input, content);
            dataMiner.AddParameter(command, "@IsSendEmail", DbType.Boolean, 0, ParameterDirection.Input, isSendEmail);
            dataMiner.AddParameter(command, "@IsMarketPlaceMessage", DbType.Boolean, 0, ParameterDirection.Input, isMarketPlaceMessage);
            dataMiner.AddParameter(command, "@IsRead", DbType.Boolean, 0, ParameterDirection.Input, isRead);
            dataMiner.AddParameter(command, "@IsActive", DbType.Boolean, 0, ParameterDirection.Input, isActive);
            dataMiner.AddParameter(command, "@IsShowInBox", DbType.Boolean, 0, ParameterDirection.Input, isShowInBox);
            dataMiner.AddParameter(command, "@IsShowOutBox", DbType.Boolean, 0, ParameterDirection.Input, isShowOutBox);
            dataMiner.AddParameter(command, "@IsReply", DbType.Boolean, 0, ParameterDirection.Input, isReply);
            dataMiner.AddParameter(command, "@ReplyToMessageID", DbType.Int32, 0, ParameterDirection.Input, replyToMessageID);
            dataMiner.AddParameter(command, "ReturnValue", DbType.Int32, 0, ParameterDirection.ReturnValue, null);
            dataMiner.Execute(command, out count);
            if (count == null)
                return false;
            else
                return (((int)count == 0) ? false : true);

        }

        public static DataTable GetPrivateMessageBySenderID(int senderID, string sortOrder, string sortDirection, int currentPageIndex, int pageSize, out int totalRecords)
        {
            DataMiner dataMiner = DataFactory.GetDataMiner();
            DbCommand command = DataFactory.GetCommand();

            dataMiner.AddParameter(command, "@SenderID", DbType.Int32, 0, ParameterDirection.Input, senderID);
            command.CommandText = "CSP_GetPrivateMessageBySenderIDPagedCount";
            totalRecords = Convert.ToInt32(dataMiner.GetOneData(command));
            totalRecords = (int)CommonUtility.FilterNull(totalRecords, typeof(int));

            
            dataMiner.AddParameter(command, "@SortOrder", DbType.String, 0, ParameterDirection.Input, sortOrder);
            dataMiner.AddParameter(command, "@SortDirection", DbType.String, 0, ParameterDirection.Input, sortDirection);
            dataMiner.AddParameter(command, "@CurrentPageIndex", DbType.Int32, 0, ParameterDirection.Input, currentPageIndex);
            dataMiner.AddParameter(command, "@PageSize", DbType.Int32, 0, ParameterDirection.Input, pageSize);
            
            command.CommandText = "CSP_GetPrivateMessageBySenderID";
            return dataMiner.GetListData(command);
        }

        public static DataTable GetPrivateMessageByReceiverID(int receiverID,string sortOrder, string sortDirection, int currentPageIndex, int pageSize, out int totalRecords)
        {
            DataMiner dataMiner = DataFactory.GetDataMiner();
            DbCommand command = DataFactory.GetCommand();

            dataMiner.AddParameter(command, "@ReceiverID", DbType.Int32, 0, ParameterDirection.Input, receiverID);

            command.CommandText = "CSP_GetPrivateMessageByReceiverIDPagedCount";
            totalRecords = Convert.ToInt32(dataMiner.GetOneData(command));
            totalRecords = (int)CommonUtility.FilterNull(totalRecords, typeof(int));

           
            dataMiner.AddParameter(command, "@SortOrder", DbType.String, 0, ParameterDirection.Input, sortOrder);
            dataMiner.AddParameter(command, "@SortDirection", DbType.String, 0, ParameterDirection.Input, sortDirection);
            dataMiner.AddParameter(command, "@CurrentPageIndex", DbType.Int32, 0, ParameterDirection.Input, currentPageIndex);
            dataMiner.AddParameter(command, "@PageSize", DbType.Int32, 0, ParameterDirection.Input, pageSize);
                
            command.CommandText = "CSP_GetPrivateMessageByReceiverID";
            return dataMiner.GetListData(command);
        }

        public static DataTable GetPrivateMessageByMessageID(int messageID)
        {
            DataMiner dataMiner = DataFactory.GetDataMiner();
            DbCommand command = DataFactory.GetCommand();
            command.CommandText = "CSP_GetPrivateMessageByMessageID";
            dataMiner.AddParameter(command, "@MessageID", DbType.Int32, 0, ParameterDirection.Input, messageID);
            return dataMiner.GetListData(command);
        }


        public static bool DeletePrivateMessage(int id)
        {
            object count = null;
            DataMiner dataMiner = DataFactory.GetDataMiner();
            DbCommand command = DataFactory.GetCommand();
            command.CommandText = "CSP_DeletePrivateMessage";
            dataMiner.AddParameter(command, "@MessageId", DbType.Int32, 0, ParameterDirection.Input, id);
            dataMiner.AddParameter(command, "ReturnValue", DbType.Int32, 0, ParameterDirection.ReturnValue, null);
            dataMiner.Execute(command, out count);
            if (count == null)
                return false;
            else
                return (((int)count == 0) ? false : true);
        }

        public static DataTable GetAllPrivateMessage()
        {
            DataMiner dataMiner = DataFactory.GetDataMiner();
            DbCommand command = DataFactory.GetCommand();
            command.CommandText = "CSP_GetAllPrivateMessage";
            return dataMiner.GetListData(command);
        }

        public static bool SetReadMails(string xml)
        {
            object count = null;
            DataMiner dataMiner = DataFactory.GetDataMiner();
            DbCommand command = DataFactory.GetCommand();
            command.CommandText = "CSP_SetReadMails";
            dataMiner.AddParameter(command, "@MessageIds", DbType.Xml, 0, ParameterDirection.Input, xml);
            dataMiner.AddParameter(command, "ReturnValue", DbType.Int32, 0, ParameterDirection.ReturnValue, null);
            dataMiner.Execute(command, out count);
            if (count == null)
                return false;
            else
                return (((int)count == 0) ? false : true);
        }
        public static bool SetUnReadMails(string xml)
        {
            object count = null;
            DataMiner dataMiner = DataFactory.GetDataMiner();
            DbCommand command = DataFactory.GetCommand();
            command.CommandText = "CSP_SetUnReadMails";
            dataMiner.AddParameter(command, "@MessageIds", DbType.Xml, 0, ParameterDirection.Input, xml);
            dataMiner.AddParameter(command, "ReturnValue", DbType.Int32, 0, ParameterDirection.ReturnValue, null);
            dataMiner.Execute(command, out count);
            if (count == null)
                return false;
            else
                return (((int)count == 0) ? false : true);
        }
        public static bool DeleteMails(string xml)
        {
            object count = null;
            DataMiner dataMiner = DataFactory.GetDataMiner();
            DbCommand command = DataFactory.GetCommand();
            command.CommandText = "CSP_DeleteMails";
            dataMiner.AddParameter(command, "@MessageIds", DbType.Xml, 0, ParameterDirection.Input, xml);
            dataMiner.AddParameter(command, "ReturnValue", DbType.Int32, 0, ParameterDirection.ReturnValue, null);
            dataMiner.Execute(command, out count);
            if (count == null)
                return false;
            else
                return (((int)count == 0) ? false : true);
        }
    }
}
